// const baseUrl = "https://pc.raz-kid.cn/api/"
const baseUrl = "http://123.56.133.241:8360/api/"
const app = getApp()
module.exports = function request(url, data = {}, method = 'GET', params='') {
  let str = '';
  if(params) {
    for(let item in params) {
      str += (item + '=' + params[item] + '&')
    }
  }
  if(str) {
    str = '?' + str
  }
  return new Promise((reslove, reject) => {
    wx.request({
      header: {
        'X-Nideshop-Token': app.globalData.token
      },
      url: baseUrl + url + str, 
      data,
      method,
      success: res => {
        if(res.statusCode >= 200 && res.statusCode <300 || res.statusCode === 304) {
          if(res.data.errno==0) {
            reslove(res.data)
          } else if (res.data.errno === 401){
            wx.navigateTo({
              url: '/pages/login/login',
            })
            reject(res)
          }else {
            wx.showToast({
              title: res.data.errmsg,
              icon: 'none'
            })
            reject(res)
          }
        }else {
          wx.showToast({
            title: res.errMsg,
          })
          reject(res)
        }
      },
      fail: err => {
        wx.showToast({
          title: err,
          icon: 'none'
        })
        reject(err)
      },
      complete: ()=> {
        wx.hideLoading()
      }
    })
  })
}